package bd_jugueteria;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class ventana extends javax.swing.JFrame {

    Connection conector;
    Statement stmt;
    log login = new log(this);

    String idEmpleado;

    public ventana() {
        initComponents();
        login.setSize(316, 345);
        login.setVisible(true);
        this.setSize(700, 500);

        this.setVisible(false);
    }

    public void setEmpleado(String em) {
        idEmpleado = em;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jtfbusca = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jButton10 = new javax.swing.JButton();
        jTextField8 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField5 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 644, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 388, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cliente-Empleado");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton2.setFont(new java.awt.Font("Papyrus", 3, 10)); // NOI18N
        jButton2.setText("CLIENTE EMPLEADO");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jButton3.setFont(new java.awt.Font("Papyrus", 3, 10)); // NOI18N
        jButton3.setText("PROVEEDOR");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 10, -1, -1));

        jButton7.setFont(new java.awt.Font("Papyrus", 3, 10)); // NOI18N
        jButton7.setText("PEDIDOS");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 10, -1, -1));

        jTabbedPane1.setFont(new java.awt.Font("Comic Sans MS", 2, 12)); // NOI18N

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jtfbusca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfbuscaKeyReleased(evt);
            }
        });

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idProducto", "Descripcion", "costo ", "Cantidad", "CostoTotal"
            }
        ));
        jScrollPane3.setViewportView(jTable3);

        jButton10.setText("Agregar");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Busca");

        jButton4.setText("Hacer venta");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jtfbusca, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3)))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jButton10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addGap(116, 116, 116))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfbusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton10)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("VENTAS", jPanel3);

        jLabel2.setText("Total");

        jLabel3.setText("abono");

        jLabel4.setText("cliente");

        jLabel5.setText("juguete");

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField4FocusLost(evt);
            }
        });
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField4KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField4KeyTyped(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTextField5.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField5KeyReleased(evt);
            }
        });

        jButton6.setText("Eliminar");

        jButton8.setText("Actualizar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Apartar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Buscar producto", "Buscar apartado" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1)
                            .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(19, 19, 19)
                                        .addComponent(jLabel3))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(27, 27, 27)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton9))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 613, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton8)
                    .addComponent(jButton9))
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("APARTADO", jPanel2);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 644, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 388, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("DEVOLUCION", jPanel4);

        getContentPane().add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, 420));

        jButton1.setFont(new java.awt.Font("Papyrus", 3, 12)); // NOI18N
        jButton1.setText("juguetes");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 10, 90, -1));

        jButton5.setFont(new java.awt.Font("Papyrus", 3, 10)); // NOI18N
        jButton5.setText("TERMINAR");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 480, -1, 41));

        jLabel1.setBackground(new java.awt.Color(153, 153, 255));
        jLabel1.setOpaque(true);
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(4, 4, 660, 530));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            // TODO add your handling code here:
            Proveedor p = new Proveedor(this, conector);
            p.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);


    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        juguetes j = null;
        try {
            j = new juguetes(this, conector);
        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }
        j.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

        cliente_empleado ce = null;
        try {
            ce = new cliente_empleado(this, conector);
        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }
        ce.setVisible(true);
        this.setVisible(false);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:

        if (jComboBox1.getSelectedIndex() == 0) {

            jTextField5.setText("");
            jTextField1.setText(null);
            jLabel5.setText("Juguete");
            jTextField4.setText(null);
            jLabel4.setText("Cliente");
            jTextField3.setText(null);
            jLabel3.setText("Abono");
            jTextField2.setText(null);
            jTextField1.setEditable(true);
            jTextField2.setEditable(true);
            jTextField3.setEditable(true);

            MostrarJuguete(" ");

            jTextField1.setForeground(Color.black);
            jTextField2.setForeground(Color.black);
            jTextField3.setForeground(Color.black);

        } else {
            jTextField5.setText("");
            MostrarApartado(" ");
            jTextField1.setText(null);

            jLabel5.setText("Cantidad a abonar");
            jTextField4.setText(null);
            jLabel4.setText("Restante");
            jTextField3.setText(null);
            jLabel3.setText("Abonado");
            jTextField2.setText(null);

            jTextField1.setEditable(false);
            jTextField2.setEditable(false);
            jTextField3.setEditable(false);

            jTextField1.setForeground(Color.blue);
            jTextField2.setForeground(Color.blue);
            jTextField3.setForeground(Color.blue);

        }

    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:


        if (jTextField1.getText().length() > 0 && jTextField2.getText().length() > 0 && jTextField3.getText().length() > 0
                && jTextField4.getText().length() > 0 && jComboBox1.getSelectedIndex() == 0) {

            try {
                try (PreparedStatement insertar = conector.prepareStatement("insert into "
                        + "apartado(precio_total, abono, idcliente, juguete_id_juguete)"
                        + " values(?,?,?,?)")) {
                    insertar.setString(1, jTextField1.getText());
                    insertar.setString(2, jTextField2.getText());
                    insertar.setString(3, jTextField3.getText());
                    insertar.setString(4, jTextField4.getText());
                    insertar.execute();
                    insertar.close();

                    Statement st = conector.createStatement();

                    ResultSet r = st.executeQuery("Select idapartado from apartado order by idapartado desc limit 1");
                    r.next();
                    System.out.println("total aqui: " + r.getString("idapartado"));

                    MostrarApartado(r.getString("idapartado"));

                    String idapartado = jTextField4.getText();
                    r = stmt.executeQuery("Select cantidad_existente from juguete where id_juguete = '" + jTextField4.getText() + "'");
                    r.next();
                    int cantActual = Integer.parseInt(r.getString("cantidad_existente"));
                    cantActual--;
                    stmt.executeUpdate("update juguete set cantidad_existente='" + cantActual + "' where id_juguete = '" + idapartado + "';");

                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("");
                    jTextField4.setText("");

                    JOptionPane.showMessageDialog(null, "Apartado agregado");

                }
            } catch (SQLException e) {
                System.out.print(e.getMessage());
                //JOptionPane.showMessageDialog(null, "ID (" + camp1.getText() + ") en uso..");
            }
        }//si campos no vacios

    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        int fila2 = jTable1.getSelectedRow();
        if (jComboBox1.getSelectedIndex() != 0) {
            if (jButton8.getText().equals("Actualizar")) {
                if (fila2 >= 0) {
                    //jug1.setText(Tjuguete.getValueAt(fila2, 0).toString());
                    float total = Float.parseFloat(jTable1.getValueAt(fila2, 1).toString());
                    float abonado = Float.parseFloat(jTable1.getValueAt(fila2, 2).toString());
                    float Restante = total - abonado;

                    int aux = (int) Restante;
                    Restante = Restante - aux;
                    if (Restante > 0.5) {
                        Restante = (float) 1.0;
                    } else if (Restante < 0.5) {
                        Restante = 0;
                    }

                    Restante = Restante + aux;

                    jTextField1.setText(jTable1.getValueAt(fila2, 1).toString());
                    jTextField2.setText(jTable1.getValueAt(fila2, 2).toString());
                    jTextField3.setText("" + Restante);
                    jButton8.setText("Guardar Cambios");
                    jLabel5.setText("Ingrese nuevo abono");
                    jTextField4.requestFocus();
                } else {
                    JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");
                }
            } else {

                if (jTextField2.getText().length() > 0 && jTextField4.getText().length() > 0) {

                    jButton8.setText("Actualizar");

                    try {

                        float tAbonado = (Float.parseFloat(jTextField2.getText()) + Float.parseFloat(jTextField4.getText()));
                        ResultSet r = stmt.executeQuery("select precio_unit from juguete where id_juguete = '" + jTable1.getValueAt(fila2, 4) + "'");
                        r.next();
                        float pTotal = Float.parseFloat(r.getString("precio_unit"));

                        if (tAbonado >= pTotal) {

                            int total;
                            r = stmt.executeQuery("Select MAX(idventatotal) as total from venta");
                            if (r.next()) {
                                total = Integer.parseInt(r.getString("total"));
                            } else {
                                total = 1;
                            }
                            total++;
                            System.out.println("anything");

                            r = stmt.executeQuery("select juguete_id_juguete from apartado where idapartado ='" + jTable1.getValueAt(fila2, 0).toString() + "'");
                            r.next();
                            String j = r.getString("juguete_id_juguete");

                            try (PreparedStatement insertar = conector.prepareStatement("insert into "
                                    + "venta(cant_juguetes,total,fecha,juguete,empleado,idventatotal) "
                                    + " values(?,?,?,?,?,?)")) {
                                insertar.setString(1, "1");//nombre
                                insertar.setString(2, "" + pTotal);//telefono
                                insertar.setString(3, "06-06-06");//sueldo
                                insertar.setString(4, j);//cargo
                                insertar.setString(5, idEmpleado);//pass
                                insertar.setString(6, "" + total);//pass
                                insertar.execute();
                            }

                            PreparedStatement pre = conector.prepareStatement("UPDATE apartado "
                                    + "SET abono='" + tAbonado + "' WHERE idapartado='" + jTable1.getValueAt(fila2, 0).toString() + "';");
                            pre.executeUpdate();
                            MostrarApartado(jTable1.getValueAt(fila2, 0).toString());

                            stmt.execute("Delete from apartado where idapartado ='" + jTable1.getValueAt(fila2, 0) + "'");
                            System.out.println("anything2");
                            JOptionPane.showMessageDialog(null, "Pagado!");

                        } else {
                            PreparedStatement pre = conector.prepareStatement("UPDATE apartado "
                                    + "SET abono='" + tAbonado + "' WHERE idapartado='" + jTable1.getValueAt(fila2, 0).toString() + "';");
                            pre.executeUpdate();
                            MostrarApartado(jTable1.getValueAt(fila2, 0).toString());
                            jTable1.setRowSelectionInterval(0, 0);
                        }

                        jTextField1.setText("");
                        jTextField2.setText("");
                        jTextField3.setText("");
                        jTextField4.setText("");

                        //JOptionPane.showMessageDialog(this, "Se Actualizo abono con Exito");
                    } catch (SQLException ex) {
                        //JOptionPane.showMessageDialog(this, "Error al Corregir");
                        System.out.print(ex.getMessage());
                    }

                }

            }

        }

    }//GEN-LAST:event_jButton8ActionPerformed

    private void jTextField5KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField5KeyReleased
        // TODO add your handling code here:

        try {
            // TODO add your handling code here:
            if (jComboBox1.getSelectedIndex() == 0) {
                stmt = conector.createStatement();

                ResultSet st = stmt.executeQuery("Select cantidad_existente from juguete where id_juguete = '" + jTextField5.getText() + "'");

                if (st.next()) {

                    int cantActual = Integer.parseInt(st.getString("cantidad_existente"));
                    if (cantActual > 0) {
                        MostrarJuguete(jTextField5.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "Producto Agotado!!");
                    }

                }//si existe

            }//son juguetes
            else // de lo contrario busca apartados
            {
                stmt = conector.createStatement();

                ResultSet st = stmt.executeQuery("Select * from apartado  where idapartado = '" + jTextField5.getText() + "'");
                if (st.next()) {
                    MostrarApartado(jTextField5.getText());
                    jTable1.setRowSelectionInterval(0, 0);
                }//si existe

            }

        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jTextField5KeyReleased

    private void jTextField4KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4KeyTyped

    private void jTextField4KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyReleased
        // TODO add your handling code here:

        try {
            // TODO add your handling code here:

            stmt = conector.createStatement();

            ResultSet st = stmt.executeQuery("Select cantidad_existente from juguete where id_juguete = '" + jTextField4.getText() + "'");

            if (st.next()) {

                int cantActual = Integer.parseInt(st.getString("cantidad_existente"));
                if (cantActual > 0) {

                    st = stmt.executeQuery("Select precio_unit from juguete where id_juguete = '" + jTextField4.getText() + "'");

                    if (st.next()) {
                        jTextField1.setText(st.getString("precio_unit"));
                    }

                    jTextField2.setText("0.0");

                } else {
                    JOptionPane.showMessageDialog(null, "Producto Agotado!!");
                }

            }//si existe

        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jTextField4KeyReleased

    private void jTextField4FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField4FocusLost

    }//GEN-LAST:event_jTextField4FocusLost

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jtfbuscaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfbuscaKeyReleased
        try {
            stmt = conector.createStatement();

            ResultSet st = stmt.executeQuery("Select cantidad_existente from juguete where id_juguete = '" + jtfbusca.getText() + "'");

            if (st.next()) {

                MostrarProductoT2(jtfbusca.getText());

            }//si existe
        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jtfbuscaKeyReleased

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:

        if (jTextField8.getText().length() > 0) {

            try {

                Statement st = conector.createStatement();

                DefaultTableModel tb = (DefaultTableModel) jTable3.getModel();

                ResultSet result = stmt.executeQuery("Select * from juguete where id_juguete = '" + jTextField8.getText() + "'");
                if (result.next()) {
                    int cantActual = Integer.parseInt(result.getString("cantidad_existente"));
                    
                        for (int i = 0; i < jTable3.getRowCount(); i++) {
                            int r = Integer.parseInt(jTable3.getValueAt(i, 0).toString());
                            if (r == Integer.parseInt(jTextField8.getText())) {
                                cantActual = cantActual - Integer.parseInt(jTable3.getValueAt(i, 3).toString());
                            break;
                            }
                                
                        }
                    
                    
                    if (cantActual > 0) {

                        boolean exist = false;
                        for (int i = 0; i < jTable3.getRowCount(); i++) {
                            int r = Integer.parseInt(jTable3.getValueAt(i, 0).toString());
                            if (r == Integer.parseInt(jTextField8.getText())) {
                                exist = true;
                            }
                        }

                        if (exist) {
                            for (int i = 0; i < jTable3.getRowCount(); i++) {
                                int r = Integer.parseInt(jTable3.getValueAt(i, 0).toString());
                                if (r == Integer.parseInt(jTextField8.getText())) {
                                    int cnt = Integer.parseInt(jTable3.getValueAt(i, 3).toString());
                                    float preciot = Float.parseFloat(jTable3.getValueAt(i, 4).toString());
                                    cnt++;
                                    float total = preciot * cnt;
                                    jTable3.setValueAt(cnt, i, 3);
                                    jTable3.setValueAt(total, i, 4);
                                }
                            }
                        } else {
                            String[] row = {jTextField8.getText(), result.getString("descripcion"), result.getString("precio_unit"), "1", result.getString("precio_unit")};
                            tb.addRow(row);
                            jTable3.setModel(tb);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Producto Agotado!!");

                    }
                }
            } catch (SQLException e) {
                System.out.print(e.getMessage());
                //JOptionPane.showMessageDialog(null, "ID (" + camp1.getText() + ") en uso..");

            }//catch

        }//if


    }//GEN-LAST:event_jButton10ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:

        
        int totalvent=0;
        ResultSet r;
        try {
            r = stmt.executeQuery("Select MAX(idventatotal) as total from venta");
            if (r.next()) {
                totalvent = Integer.parseInt(r.getString("total"));
            } else {
                totalvent = 1;
            }
            totalvent++;
        } catch (SQLException ex) {
            Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        for (int i = 0; i < jTable3.getRowCount(); i++) {

            try (PreparedStatement insertar = conector.prepareStatement("insert into "
                    + "venta(cant_juguetes,total,fecha,juguete,empleado,idventatotal) "
                    + " values(?,?,?,?,?,?)")) {
                int cantidadtotal = Integer.parseInt(jTable3.getValueAt(i, 3).toString());
                float preciot = Float.parseFloat(jTable3.getValueAt(i, 4).toString());
                int idjug = Integer.parseInt(jTable3.getValueAt(i, 0).toString());

                insertar.setString(1, "" + cantidadtotal);//nombre
                insertar.setString(2, "" + preciot);//telefono
                insertar.setString(3, "06-06-06");//sueldo
                insertar.setString(4, "" + idjug);//cargo
                insertar.setString(5, idEmpleado);//pass
                insertar.setString(6, "" + totalvent);//pass
                insertar.execute();
                
                        System.err.println("actualizo");
                      
                        r = stmt.executeQuery("Select cantidad_existente from juguete where id_juguete = '" + jTextField8.getText() + "'");
                        r.next();
                        int cantAct = Integer.parseInt(r.getString("cantidad_existente"));
                        cantAct = cantAct - cantidadtotal;
                        stmt.executeUpdate("update juguete set cantidad_existente='" + cantAct + "' where id_juguete = '" + idjug + "';");

                
            } catch (SQLException ex) {
                Logger.getLogger(ventana.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        
        jTable3.removeAll();
        

    }//GEN-LAST:event_jButton4ActionPerformed

    public static void main(String args[]) {

        ventana v = new ventana();
    }

    public void conectar() {

    }

    public void setSTMT(Statement stm) {
        stmt = stm;
    }

    public void setConnection(Connection conect) {
        conector = conect;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jtfbusca;
    // End of variables declaration//GEN-END:variables

    private void MostrarApartado(String apartado) {

        System.out.println("total: " + apartado);

        DefaultTableModel ModeloJuguete = new DefaultTableModel();
        ModeloJuguete.addColumn("ID Apartado");
        ModeloJuguete.addColumn("Total ");
        ModeloJuguete.addColumn("Abono");
        ModeloJuguete.addColumn("Cliente");
        ModeloJuguete.addColumn("juguete");

        ModeloJuguete.addColumn("Restante");

        jTable1.setModel(ModeloJuguete);
        jTable1.isCellEditable(0, 0);

        String sql = "";
        if (apartado.equals("")) {

            sql = "SELECT * FROM apartado";
        } else {
            sql = "SELECT * FROM apartado WHERE idapartado ='" + apartado + "'";
        }
        String[] datosjug = new String[6];
        try {
            Statement st = conector.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while (rs.next()) {
                float total = Float.parseFloat(rs.getString("precio_total"));
                float abono = Float.parseFloat(rs.getString("abono"));

                float rest = total - abono;
                datosjug[0] = rs.getString(1);
                datosjug[1] = rs.getString(2);
                datosjug[2] = rs.getString(3);
                datosjug[3] = rs.getString(4);
                datosjug[4] = rs.getString(5);
                datosjug[5] = "" + rest;

                ModeloJuguete.addRow(datosjug);

            }
            jTable1.setModel(ModeloJuguete);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void MostrarJuguete(String juguete) {

        System.out.println("total: " + juguete);

        DefaultTableModel ModeloJuguete = new DefaultTableModel();
        ModeloJuguete.addColumn("ID juguete");
        ModeloJuguete.addColumn("Descripcion ");
        ModeloJuguete.addColumn("Marca");
        ModeloJuguete.addColumn("Precio unitario");
        ModeloJuguete.addColumn("Area");
        ModeloJuguete.addColumn("Cantidad existente");
        ModeloJuguete.addColumn("Proveedor");
        ModeloJuguete.addColumn("Cantidad total");
        jTable1.setModel(ModeloJuguete);
        jTable1.isCellEditable(0, 0);

        String sql = "";
        if (juguete.equals("")) {

            sql = "SELECT * FROM juguete";
        } else {
            sql = "SELECT * FROM juguete WHERE id_juguete ='" + juguete + "'";
        }
        String[] datosjug = new String[8];
        try {
            Statement st = conector.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while (rs.next()) {
                datosjug[0] = rs.getString(1);
                datosjug[1] = rs.getString(2);
                datosjug[2] = rs.getString(3);
                datosjug[3] = rs.getString(4);
                datosjug[4] = rs.getString(5);
                datosjug[5] = rs.getString(6);
                datosjug[6] = rs.getString(7);
                datosjug[7] = rs.getString(8);

                ModeloJuguete.addRow(datosjug);

            }
            jTable1.setModel(ModeloJuguete);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }

    private void MostrarProductoT2(String juguete) {

        System.out.println("total: " + juguete);

        DefaultTableModel ModeloJuguete = new DefaultTableModel();
        ModeloJuguete.addColumn("ID juguete");
        ModeloJuguete.addColumn("Descripcion ");
        ModeloJuguete.addColumn("Marca");
        ModeloJuguete.addColumn("Precio unitario");
        ModeloJuguete.addColumn("Area");
        ModeloJuguete.addColumn("Cantidad existente");
        ModeloJuguete.addColumn("Proveedor");
        ModeloJuguete.addColumn("Cantidad total");
        jTable2.setModel(ModeloJuguete);
        jTable2.isCellEditable(0, 0);

        String sql = "";
        if (juguete.equals("")) {

            sql = "SELECT * FROM juguete";
        } else {
            sql = "SELECT * FROM juguete WHERE id_juguete ='" + juguete + "'";
        }
        String[] datosjug = new String[8];
        try {
            Statement st = conector.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while (rs.next()) {
                datosjug[0] = rs.getString(1);
                datosjug[1] = rs.getString(2);
                datosjug[2] = rs.getString(3);
                datosjug[3] = rs.getString(4);
                datosjug[4] = rs.getString(5);
                datosjug[5] = rs.getString(6);
                datosjug[6] = rs.getString(7);
                datosjug[7] = rs.getString(8);

                ModeloJuguete.addRow(datosjug);

            }
            jTable2.setModel(ModeloJuguete);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }
}
